package com.woohua.security.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/***
 * @title UserDetailServiceImpl
 * @description
 * @author baiji
 * @version 1.0.0
 * @create 2023/11/23 15:39
 **/
@Slf4j
public class UserDetailServiceImpl implements UserDetailsService {

    public static final String USER = "user";
    public static final String ADMIN = "admin";
    public static final String ONE = "one";
    public static final String TWO = "two";
    public static final String THREE = "three";

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 在这里根据用户名查询用户信息，并返回一个实现了 UserDetails 接口的对象
        // 需要根据具体情况进行数据库查询、密码加密等操作
        // 在这个示例中，我们假设数据库中存在一个名为 "user" 的用户，密码为 "password"，拥有 "USER" 角色
        if (USER.equals(username)) {
            String password = new BCryptPasswordEncoder().encode("123456");
            log.info("user password: {}", password);
            return User.builder()
                    .username(USER)
                    .password(password)
                    .roles("USER").build();
        } else if (ADMIN.equals(username)) {
            String password = new BCryptPasswordEncoder().encode("111111");
            log.info("admin password: {}", password);
            return User.builder()
                    .username(ADMIN)
                    .password(password)
                    .roles("ADMIN").build();
        } else if (ONE.equals(username)) {
            String password = new BCryptPasswordEncoder().encode("111111");
            log.info("one password: {}", password);
            return User.builder()
                    .username(ONE)
                    .password(password)
                    .roles("ONE").build();
        } else if (TWO.equals(username)) {
            String password = new BCryptPasswordEncoder().encode("111111");
            log.info("two password: {}", password);
            return User.builder()
                    .username(TWO)
                    .password(password)
                    .roles("TWO").build();
        } else if (THREE.equals(username)) {
            String password = new BCryptPasswordEncoder().encode("111111");
            log.info("three password: {}", password);
            return User.builder()
                    .username(THREE)
                    .password(password)
                    .roles("THREE").build();
        } else {
            throw new UsernameNotFoundException("User not found");
        }
    }
}
